﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;
using Microsoft.Phone.Shell;
using System.ComponentModel;
using RingSong.Function;
using RingSong.ViewModels;


namespace RingSong
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<RingViewModel> Items { get; set; }
        public ObservableCollection<RingViewModel> Items2 { get; set; }
        public ObservableCollection<RingViewModel> Items3 { get; set; }
        public ObservableCollection<RingViewModel> Items4 { get; set; }
        public ObservableCollection<RingViewModel> Items5 { get; set; }

        public MainViewModel()
        {
            this.Items = new ObservableCollection<RingViewModel>();
            this.Items2 = new ObservableCollection<RingViewModel>();
            this.Items3 = new ObservableCollection<RingViewModel>();
            this.Items4 = new ObservableCollection<RingViewModel>();
            this.Items5 = new ObservableCollection<RingViewModel>();
        }

        //初始化数据
        public void LoadData()
        {
            string _url = UrlMark.MarkUrl(UrlMark.url_getlst) + "&p=" + p[0]
                + "&flag=1&typeid=0";
            GetData(_url, 1);
        }

        //获取数据
        public void GetData(string _url, int f)
        {
            MyPage._progress.IsVisible = true;
            WebClient webClient = new WebClient();
            webClient.OpenReadCompleted += (_, __) =>
            {
                if (__.Error != null)
                {
                    MessageBox.Show("网络错误，请检查网络是否连接好！");
                    MyPage._progress.IsVisible = false; return;
                };
                using (Stream streamResult = __.Result)
                {
                    XElement root = XElement.Load(streamResult);
                    var tests = from el in root.Elements("resultinfo") select el;
                    foreach (var a in tests)
                    {
                        if (a.Attribute("result").Value == "1") //成功
                        {
                            var rings = from el in root.Elements("ring") select el;
                            ObservableCollection<RingViewModel> items = new ObservableCollection<RingViewModel>();
                            if (f == 1) items = Items;
                            else if (f == 2) items = Items2;
                            else if (f == 3) items = Items3;
                            else if (f == 5) items = Items5;
                            int i = items.Count + 1;
                            foreach (var r in rings) //遍历rings
                            {
                                items.Add(new RingViewModel()
                                {
                                    Tag = i.ToString(),
                                    Infoid = r.Element("infoid").Value,
                                    Title = Com.XMLstr(r.Element("title").Value),
                                    Totaldown = r.Element("totaldown").Value,
                                    Downurl = r.Element("downurl").Value,
                                    Singer = r.Element("singer").Value,
                                });
                                i++;
                            }
                            var page = from el in root.Elements("page") select el;
                            foreach (var pe in page)
                            {
                                int p = Convert.ToInt32(pe.Attribute("p").Value);
                                isnextpage[f - 1] = Convert.ToInt32(pe.Attribute("total").Value) > p * 10 ? 1 : 0;
                            }
                            if (f == 3 && Items3.Count == 0)
                            {
                                MainPage.SearchFail.Visibility = Visibility.Visible;
                                MainPage.SearchFail.Text = "抱歉，暂时没有关于“" + MainPage.Txt.Text + "”的铃声。";
                            }
                            else if (f == 5 && Items5.Count == 0)
                            {
                                MainPage.TypeFail.Visibility = Visibility.Visible;
                            }
                            this.IsDataLoaded[f - 1] = true;
                            MyPage._progress.IsVisible = false;
                        }
                    }
                }
            };
            webClient.OpenReadAsync(new Uri(_url), webClient);
        }


        #region 页码
        private int[] p = { 1, 1, 1, 1, 1 };
        private int[] isnextpage = { 1, 1, 1, 1, 1 };
        private int[] pagesize = { 10, 10, 10, 10, 10 };

        public int[] Pagesize
        {
            get { return pagesize; }
            set { pagesize = value; }
        }

        private bool[] isDataLoaded = { false, false, false, false, false };

        //初始化状态
        public bool[] IsDataLoaded
        {
            get
            {
                return isDataLoaded;
            }
            private set
            {
                isDataLoaded = value;
            }
        }

        public int[] P
        {
            get { return p; }
            set { p = value; }
        }


        public int[] Isnextpage
        {
            get { return isnextpage; }
        }
        #endregion

        #region 通知事件
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}